package com.jdone.compus.controller;

import com.jdone.compus.dto.ApiResponse;
import com.jdone.compus.model.GiftSubmission;
import com.jdone.compus.service.GiftSubmissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/gift-submissions")
@Api(value = "礼物领取接口", tags = "礼物领取接口")
public class GiftSubmissionController {

    @Autowired
    private GiftSubmissionService service;

    @ApiOperation(value = "创建一个新的礼品提交记录", notes = "根据传入的礼品提交信息创建新的记录")
    @PostMapping
    public ResponseEntity<ApiResponse> createGiftSubmission(@RequestBody GiftSubmission giftSubmission) {
        GiftSubmission created = service.createGiftSubmission(giftSubmission);
        return ResponseEntity
                .status(HttpStatus.CREATED)
                .body(new ApiResponse(true, "Gift submission created successfully", created));
    }

    @ApiOperation(value = "获取所有礼品提交记录", notes = "返回所有礼品提交记录的列表")
    @GetMapping
    public ResponseEntity<ApiResponse> getAllGiftSubmissions() {
        List<GiftSubmission> submissions = service.getAllGiftSubmissions();
        return ResponseEntity
                .ok(new ApiResponse(true, "Fetched all gift submissions", submissions));
    }

    @ApiOperation(value = "根据ID获取礼品提交记录", notes = "根据提供的ID返回对应的礼品提交记录")
    @GetMapping("/{id}")
    public ResponseEntity<ApiResponse> getGiftSubmissionById(@PathVariable Long id) {
        Optional<GiftSubmission> submission = service.getGiftSubmissionById(id);
        return submission.map(s -> ResponseEntity
                        .ok(new ApiResponse(true, "Gift submission found", s)))
                .orElseGet(() -> ResponseEntity
                        .status(HttpStatus.NOT_FOUND)
                        .body(new ApiResponse(false, "Gift submission not found")));
    }

    @ApiOperation(value = "更新指定ID的礼品提交记录", notes = "根据提供的ID和更新的信息更新礼品提交记录")
    @PutMapping("/{id}")
    public ResponseEntity<ApiResponse> updateGiftSubmission(@PathVariable Long id, @RequestBody GiftSubmission giftSubmission) {
        GiftSubmission updated = service.updateGiftSubmission(id, giftSubmission);
        return ResponseEntity
                .ok(new ApiResponse(true, "Gift submission updated successfully", updated));
    }

    @ApiOperation(value = "删除指定ID的礼品提交记录", notes = "根据提供的ID删除对应的礼品提交记录")
    @DeleteMapping("/{id}")
    public ResponseEntity<ApiResponse> deleteGiftSubmission(@PathVariable Long id) {
        service.deleteGiftSubmission(id);
        return ResponseEntity
                .status(HttpStatus.NO_CONTENT)
                .body(new ApiResponse(true, "Gift submission deleted successfully"));
    }


}


